/**
 * 检查cron表达式是否合法
 *
 * @param cronExpression cron表达式字符串
 * @param digits cron表达式的位数，默认为6
 * @returns 如果cron表达式合法，则返回true；否则返回false
 */
export function cronCheck(cronExpression: string, digits = 6) {
  if (!cronExpression)
    return false
  const cronParts = cronExpression.split(' ')
  if (cronParts.length > digits || cronParts.length < 5) {
    return false
  }
  return true
}

/**
 * @param cronExpression
 * @return '--' | 'error! 不合法的cron表达式' | string
 */
export function cronParserToCn(cronExpression?: string, digits = 6) {
  if (!cronExpression)
    return '--'
  const cronParts = cronExpression.split(' ')
  if (cronParts.length > digits || cronParts.length < 5)
    return 'error! 不合法的cron表达式'

  const seconds = parseCronPart(cronParts[0], '秒钟')
  const minutes = parseCronPart(cronParts[1], '分钟')
  const hours = parseCronPart(cronParts[2], '小时')
  const dayOfMonth = parseCronPart(cronParts[3], '天')
  const month = parseCronPart(cronParts[4], '月')
  const dayOfWeek = parseCronPart(cronParts[5], '星期')
  const year = parseCronPart(cronParts[6], '年')

  let parsedExpression = ''

  if (year)
    parsedExpression += `在${year} `

  if (month)
    parsedExpression += `${month} `

  if (dayOfMonth && !dayOfWeek)
    parsedExpression += `${dayOfMonth} `

  if (dayOfMonth && dayOfWeek)
    parsedExpression += `${dayOfMonth} ${dayOfWeek} `

  if (!dayOfMonth && dayOfWeek)
    parsedExpression += `${dayOfWeek} `

  if (hours)
    parsedExpression += `${hours} `

  if (minutes)
    parsedExpression += `${minutes} `

  if (seconds)
    parsedExpression += `${seconds} `

  parsedExpression += '触发'

  return parsedExpression.trim()
}

/**
 * 解析cron表达式的某个部分，并返回其对应的中文描述
 *
 * @param cronPart cron表达式的某个部分
 * @param partName cron表达式部分对应的名称（如“分钟”、“小时”等）
 * @returns 返回cron表达式的中文描述
 */
function parseCronPart(cronPart: string, partName: string) {
  if (!cronPart)
    return ''
  let parsedPart = ''
  if (cronPart === '*') {
    parsedPart = `每${partName}里`
  }
  else if (cronPart.includes('-')) {
    const [start, end] = cronPart.split('-')
    if (partName == '星期')
      parsedPart = `从${partName}${start}到${partName}${end}里`
    else parsedPart = `从${start}到${end}的每${partName}里`
  }
  else if (cronPart.includes('/')) {
    const [value, interval] = cronPart.split('/')
    if (partName == '星期')
      parsedPart = `从${partName}${value}开始的每隔${interval}天里`
    else
      parsedPart = `从第${value}${partName}开始的每隔${interval}${partName}里`
  }
  else if (cronPart.includes(',')) {
    const values = cronPart.split(',')
    parsedPart = ''
    values.forEach((value, index) => {
      if (partName == '星期')
        parsedPart += `${partName}${value}的时候`
      else parsedPart += `第${value}${partName}的时候`

      if (index !== values.length - 1)
        parsedPart += '和'
    })
  }
  else if (cronPart.startsWith('L')) {
    parsedPart = `最后一个${partName}里`
  }
  else if (cronPart.endsWith('L')) {
    const value = cronPart.slice(0, -1)
    if (partName == '星期')
      parsedPart = `当月最后一个${partName}${value}里`
    else parsedPart = `${value}号的最后一个${partName}里`
  }
  else if (cronPart.includes('#')) {
    const [value, weekday] = cronPart.split('#')
    parsedPart = `第${value}周的${partName}${weekday}里`
  }
  else if (cronPart.includes('W')) {
    const value = cronPart.slice(0, -1)
    parsedPart = `最接近${value}号的工作日里`
  }
  else if (cronPart === '?') {
    parsedPart = ''
  }
  else {
    if (partName == '星期')
      parsedPart = `${partName}${cronPart}里`
    else parsedPart = `第${cronPart}${partName}里`
  }
  return parsedPart
}
